﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using Hardcodet.Wpf.TaskbarNotification;
using WebBrowserOnTransparentWindow;

namespace QuickConsole
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool balloonShown;

        EventWaitHandle h = new EventWaitHandle(false, EventResetMode.ManualReset);

        public MainWindow()
        {
            InitializeComponent();

            var webBrower = new WebBrowserOverlay(container);

            taskbarIcon.LeftClickCommand = new ShowPowerGUICommand();
            taskbarIcon.LeftClickCommandParameter = this;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void Minimize_Click(object sender, RoutedEventArgs e)
        {
            Hide();

            if (!balloonShown)
            {
                taskbarIcon.ShowBalloonTip("Still here!", "I'm still running! If you want to open the QuickConsole just click on me.", BalloonIcon.Info);
            }

            balloonShown = true;

            
        }

        private void Maximize_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;    
            }
            else
            {
                WindowState = WindowState.Normal;    
            }
            
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            h.Set();
            Environment.Exit(0);
        }


    }

    public class ShowPowerGUICommand : ICommand
    {
        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            if (parameter is Window)
            {
                var window = parameter as Window;

                if (window.Visibility == Visibility.Hidden)
                {
                    window.Show();    
                }

                if (window.WindowState == WindowState.Minimized)
                {
                    window.WindowState = WindowState.Maximized;
                }
            }
        }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public bool CanExecute(object parameter)
        {
            if (parameter is Window)
            {
                return (parameter as Window).Visibility == Visibility.Hidden ||
                       (parameter as Window).WindowState == WindowState.Minimized;
            }
            return false;
        }

        public event EventHandler CanExecuteChanged;
    }
}
